﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft
{

    /// <summary>
    /// 信息集合   List<Information>
    /// </summary>
    [Serializable]
    public sealed class InformationCollection : baseObject, IEnumerable<Information>, IEnumerable, IList<Information>, ICollection<Information>
    {
        /// <summary>
        /// informations 集合变动之后 发出事件通知
        /// </summary>
        public event InformationListChangedEventHandle InformationListChanged;

        public InformationCollection()
        {
            this.mInformations = new List<Information>();
        }

        public InformationCollection(params Information[] infos) : this()
        {
            this.AddRange(infos);
        }

        public void Add(Information info)
        {
            this.mInformations.Add(info);
            this.OnInformationListChanged();
        }

        public void AddRange(IEnumerable<Information> infos)
        {
            if (infos != null && infos.Count<Information>() > 0)
            {
                this.mInformations.AddRange(infos);
                this.OnInformationListChanged();
            }
        }

        public void Clear()
        {
            this.mInformations.Clear();
            this.OnInformationListChanged();
        }

        public Information this[int index]
        {
            get
            {
                return this.mInformations[index];
            }
        }

   
        public int Count
        {
            get
            {
                return this.mInformations.Count;
            }
        }

        public IEnumerator<Information> GetEnumerator()
        {
            return this.mInformations.GetEnumerator();
        }

  

      
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.mInformations.GetEnumerator();
        }




        public IEnumerable<IGrouping<InformationType, Information>> GroupBy()
        {
            return from e in this.mInformations
                   group e by e.Type;
        }

        public IEnumerable<Information> FindByType(InformationType type)
        {
            return this.mInformations.FindAll((Information e) => e.Type == type);
        }

        public string Text
        {
            get
            {
                return string.Join("\r\n", this.mInformations.ConvertAll<string>((Information e) => string.Format("[{0}]{1}", e.Type.ToString(), e.Text)));
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }


        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }


        public object SyncRoot
        {
            get
            {
                return true;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return true;
            }
        }

        Information IList<Information>.this[int index]
        {
            get
            {
                return this.mInformations[index];
            }
            set
            {
                this.mInformations[index] = value;
            }
        }

        public override object Clone()
        {
            InformationCollection informationCollection = base.Clone<InformationCollection>();
            informationCollection.mInformations = new List<Information>();
            if (this.mInformations.Count > 0)
            {
                informationCollection.mInformations.AddRange(this.mInformations);
            }
            return informationCollection;
        }

     
        public override string ToString()
        {
            return this.Text;
        }

 
        private void OnInformationListChanged()
        {
            if (this.InformationListChanged != null)
            {
                this.InformationListChanged(this.mInformations);
            }
        }

        public int IndexOf(Information item)
        {
            return this.mInformations.IndexOf(item);
        }

  
        public void Insert(int index, Information item)
        {
            this.mInformations.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            this.mInformations.RemoveAt(index);
        }

        public bool Contains(Information item)
        {
            return this.mInformations.Contains(item);
        }


        public void CopyTo(Information[] array, int arrayIndex)
        {
            this.mInformations.CopyTo(array, arrayIndex);
        }

     
        public bool Remove(Information item)
        {
            return this.mInformations.Remove(item);
        }


        private List<Information> mInformations;
    }
}
